package com.excellent.item.controller;

import com.excellent.common.PageResult;
import com.excellent.item.pojo.Brand;
import com.excellent.item.pojo.Category;
import com.excellent.item.service.BrandService;
import com.excellent.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Xxacker
 */
@Controller
@RequestMapping("brand")
public class BrandController {

    @Autowired
    private BrandService brandService;
    @Autowired
    private CategoryService categoryService;

    /**【后台 - 品牌查询功能】
     * 根据查询条件分页并排序查询品牌信息
     * @param key       // 搜索条件，required = false
     * @param page      // 当前页
     * @param rows      // 每页大小
     * @param sortBy    // 排序字段
     * @param desc      // 是否降序
     * @return
     */
    @GetMapping("page")
    public ResponseEntity<PageResult<Brand>> queryBrandsByPage(
            @RequestParam(value = "key", required = false)String key,
            @RequestParam(value = "page", defaultValue = "1")Integer page,
            @RequestParam(value = "rows", defaultValue = "10")Integer rows,
            @RequestParam(value = "sortBy", required = false)String sortBy,
            @RequestParam(value = "desc", required = false)Boolean desc
    ){
        PageResult<Brand> result = this.brandService.queryBrandsByPage(key, page, rows, sortBy, desc);
        if (CollectionUtils.isEmpty(result.getItems())){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 【新增品牌功能】
     * @param brand
     * @param cids   使用 @RequestParam 来接收对象中的参数 cids
     */
    @PostMapping
    public ResponseEntity<Void> saveBrand(Brand brand, @RequestParam("cids") List<Long> cids){
        this.brandService.saveBrand(brand, cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**【品牌回显功能】
     * 根据品牌回显分类数据
     * 【因为在品牌的回显数据中，需要回显属于哪个分类，所以需要查询分类和品牌的对应关系】
     * @param brandId
     * @return
     */
    @GetMapping("id/{id}")
    public ResponseEntity<List<Category>> queryCategoryById(@PathVariable("id")Long brandId){
        List<Category> list = this.categoryService.queryByBrandId(brandId);
        if (list == null || list.size() < 1) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(list);
    }

    /**
     * 【品牌编辑功能】
     * @param brand
     * @param cids   使用 @RequestParam 来接收对象中的参数 cids
     */
    @PutMapping
    public ResponseEntity<Void> updateBrand(Brand brand, @RequestParam("cids") List<Long> cids){
        this.brandService.updateBrand(brand, cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 【删除品牌功能】
     * @param brand
     */
    @PostMapping("delete")
    public ResponseEntity<Void> deleteBrand(Brand brand){
        this.brandService.deleteBrand(brand);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 【新增商品功能 -> 根据商品类目选择品牌】
     * 根据分类 ID 查询品牌信息列表
     * 【因为在新增商品时，需要先选择商品类目，再选择商品品牌】
     * @param cid
     * @return
     */
    @GetMapping("cid/{cid}")
    public ResponseEntity<List<Brand>> queryBrandsByCid(@PathVariable("cid")Long cid){
        List<Brand> brands = this.brandService.queryBrandsByCid(cid);
        if (CollectionUtils.isEmpty(brands)) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(brands);
    }

    /**【商城页面 -> 】
     * 这里的参数为占位符参数
     * 根据分类 ID 查询品牌信息
     **/
    @GetMapping("{id}")
    public ResponseEntity<Brand> queryBrandById(@PathVariable("id")Long id){
        Brand brand = this.brandService.queryBrandsById(id);
        if (brand == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(brand);
    }
}
